package com.sls.web.manager.action.game;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;

import com.sls.web.manager.action.SuperAction;
import com.sls.web.manager.common.Constants;
import com.sls.web.manager.common.FilterStringSpaces;
import com.sls.web.manager.execption.ManagerException;
import com.sls.web.manager.game.pojo.OhFlyBrand;
import com.sls.web.manager.game.pojo.OhFlyUserInfo;
import com.sls.web.manager.game.pojo.SyncInfo;
import com.sls.web.manager.game.querybean.OhFlyBrandQuery;
import com.sls.web.manager.game.service.OhFlyService;
import com.sls.web.manager.page.Pagination;

public class OhflyBrandSyncAction extends SuperAction {

	private static final long serialVersionUID = 1L;

	@Resource(name = "ohFlyService")
	private OhFlyService ohFlyService;
	private OhFlyBrand ohFlyBrand;
	private OhFlyUserInfo ohFlyUserInfo;
	private OhFlyBrandQuery ohFlyBrandQuery;
	private Pagination<OhFlyBrand> page;
	private List<OhFlyUserInfo> users;

	/**
	 * 列表
	 * 
	 * @return
	 * @throws Exception
	 */
	public String list() throws Exception {
		try {
			if (ohFlyBrandQuery == null) {
				ohFlyBrandQuery = new OhFlyBrandQuery();
				ohFlyBrandQuery.setPageSize(Constants.INIT_PAGESIZE);
				ohFlyBrandQuery.setCurrentPage(Constants.INIT_CURRENTPAGE);
			}
			String classid = ohFlyBrandQuery.getClassid();
			String cardid = ohFlyBrandQuery.getCardid();
			String cardname = ohFlyBrandQuery.getCardname();
			classid = FilterStringSpaces.checkStringHasSpace(classid);
			cardid = FilterStringSpaces.checkStringHasSpace(cardid);
			cardname = FilterStringSpaces.checkStringHasSpace(cardname);
			ohFlyBrandQuery.setCardid(cardid);
			ohFlyBrandQuery.setCardname(cardname);
			ohFlyBrandQuery.setClassid(classid);
			page = ohFlyService.queryOhFlyBrand(ohFlyBrandQuery,
					getCurrentOperator());
			users = ohFlyService.queryOhFlyUserInfo(getCurrentOperator());
		} catch (ManagerException e) {
			addActionError(e.getMessage());
		}
		return LIST;
	}

	/**
	 * 同步
	 * 
	 * @return
	 * @throws Exception
	 */
	public String sync() throws Exception {
		try {
			ohFlyUserInfo.setVersion(Constants.OH_FLY_VERSION);
			List<OhFlyUserInfo> userList = ohFlyService
					.queryOhFlyUserInfo(getCurrentOperator());
			String username = ohFlyUserInfo.getUserid();
			if(StringUtils.isNotEmpty(username)){
				username = username.trim();
			}
			if (userList != null) {
				for (OhFlyUserInfo user : userList) {
					String userid = user.getUserid();
					if (StringUtils.isNotEmpty(userid)) {
						userid = userid.trim();
					}
					if (username.equals(userid)) {
						String userpws = user.getUserpws();
						if (StringUtils.isNotEmpty(userpws)) {
							userpws = userpws.trim();
						}
						ohFlyUserInfo.setUserpws(userpws);
						break;
					}
				}

			}
			SyncInfo syncInfo = ohFlyService.brandSynchronize(ohFlyUserInfo,
					getCurrentOperator());
			if (syncInfo != null) {
				String retCode = syncInfo.getRetcode();
				if (StringUtils.isNotEmpty(retCode) && !"1".equals(retCode)) {
					flag = false;
					addActionMessage(syncInfo.getErr_msg());
				} else {
					int resNum = syncInfo.getResNum();
					addActionMessage(String.valueOf(resNum));
				}
			}
			redirectionUrl = "ohfly_brand_sync!list.htm";
		} catch (ManagerException e) {
			flag = false;
			addActionError(e.getMessage());
		}
		return "sync_success";
	}

	public OhFlyService getOhFlyService() {
		return ohFlyService;
	}

	public void setOhFlyService(OhFlyService ohFlyService) {
		this.ohFlyService = ohFlyService;
	}

	public OhFlyBrand getOhFlyBrand() {
		return ohFlyBrand;
	}

	public void setOhFlyBrand(OhFlyBrand ohFlyBrand) {
		this.ohFlyBrand = ohFlyBrand;
	}

	public OhFlyUserInfo getOhFlyUserInfo() {
		return ohFlyUserInfo;
	}

	public void setOhFlyUserInfo(OhFlyUserInfo ohFlyUserInfo) {
		this.ohFlyUserInfo = ohFlyUserInfo;
	}

	public OhFlyBrandQuery getOhFlyBrandQuery() {
		return ohFlyBrandQuery;
	}

	public void setOhFlyBrandQuery(OhFlyBrandQuery ohFlyBrandQuery) {
		this.ohFlyBrandQuery = ohFlyBrandQuery;
	}

	public Pagination<OhFlyBrand> getPage() {
		return page;
	}

	public void setPage(Pagination<OhFlyBrand> page) {
		this.page = page;
	}

	public List<OhFlyUserInfo> getUsers() {
		return users;
	}

	public void setUsers(List<OhFlyUserInfo> users) {
		this.users = users;
	}

}
